package com.aaa.service.impl;

import com.aaa.entity.Staff;
import com.aaa.mapper.CheckMapper;
import com.aaa.mapper.OrderMapper;
import com.aaa.mapper.OrderdetailsMapper;
import com.aaa.mapper.RoomMapper;
import com.aaa.service.CheckService;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class CheckServiceImpl implements CheckService {
    @Resource
    private CheckMapper checkMapper;
    @Resource
    private OrderdetailsMapper orderdetailsMapper;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private RoomMapper roomMapper;

    @Override
    public Integer addcheck(Authentication authentication, Integer odid, String enddate, String cname, String cphone, String idcard, String checkin, Integer oid, Integer rid) {

        Integer staff = ((Staff) authentication.getPrincipal()).getId();
        Integer result = checkMapper.addcheck(staff,odid, enddate, cname, cphone, idcard, checkin);
        orderdetailsMapper.addroom(rid, odid);
        roomMapper.changestate(rid);
        if (result > 0) {
            orderdetailsMapper.checkstate(odid, "已入住");
        }
        if (oid != null) {
            Map<String, Object> map1 = orderdetailsMapper.findchecknum(oid, "已入住");
            Map<String, Object> map2 = orderdetailsMapper.findnum(oid);
            if (map1.get("num") == map2.get("num")) {
                orderMapper.changestate("已入住", oid);
            }
        }
        return result;
    }


}
